package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.*;
import com.baomidou.ant.mapper.LuckDrawMapper;
import com.baomidou.ant.service.*;
import com.baomidou.ant.utils.MyStringUtils;
import com.baomidou.ant.vo.Response.ResponseLuckDrawOfPointsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * 对于奖品发放
 * 1，判断是积分抽奖还是钻石抽奖
 * 2,1是积分奖励，2是道具奖励，3是饰品奖励，4是稀有奖励
 * 3,积分奖励使用了字符串提取数字的方法，得到奖金额，在赋予用户
 * 4，其他奖统一发放到背包,关联的userBag(背包信息)，bagContent(背包内容物),信息要更改
 *
 * @author jobob
 * @since 2021-03-24
 */
@Service
public class LuckDrawServiceImpl extends ServiceImpl<LuckDrawMapper, LuckDraw> implements ILuckDrawService {
    @Autowired
    private IPrizePointsService prizePointsService;
    @Autowired
    private IPrizeDiamondsService prizeDiamondsService;
    @Autowired
    private IBagContentService bagContentService;
    @Autowired
    private IUserBagService userBagService;
    @Autowired
    private IPointsStoreService pointsStoreService;

    @Override
    public List<ResponseLuckDrawOfPointsVo> list(Integer userId, String type) {
        QueryWrapper<LuckDraw> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("prize_source",type);
        List<LuckDraw> list=list(queryWrapper);

        List<ResponseLuckDrawOfPointsVo> voList=new ArrayList<>();
        for(LuckDraw luckDraw:list){
            ResponseLuckDrawOfPointsVo vo=new ResponseLuckDrawOfPointsVo();
            BeanUtils.copyProperties(luckDraw,vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public void pullPrize(Integer result, String type, User user) {
        //先判断是什么抽奖类型
        if(type=="积分抽奖"){
            PrizePoints prize=prizePointsService.getById(result);
            //判断奖品类型
            if(prize.getPrizeType()==1){
                //积分额度获取
                Integer points=Integer.parseInt(MyStringUtils.pullNumber(prize.getPrizeName()));
                user.setPoints(user.getPoints()+ points);
            }
            if(prize.getPrizeType()==2||prize.getPrizeType()==3||prize.getPrizeType()==4){
                QueryWrapper<BagContent> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("user_id",user.getId());
                queryWrapper.eq("goods_name",prize.getPrizeName());
                BagContent bagContent=bagContentService.getOne(queryWrapper);
                //判断有无这个道具,没有则新加一条数据
                if(bagContent==null){
                    //得到道具详情信息
                    QueryWrapper<PointsStore> pointsStoreQueryWrapper=new QueryWrapper<>();
                    pointsStoreQueryWrapper.eq("goods_name",prize.getPrizeName());
                    PointsStore goods=pointsStoreService.getOne(pointsStoreQueryWrapper);
                    //添加背包内容物
                    BagContent bagContent1=new BagContent();
                    bagContent1.setGoodsId(goods.getId());
                    bagContent1.setGoodsName(goods.getGoodsName());
                    bagContent1.setAmount(1);
                    bagContent1.setUserId(user.getId());
                    //背包加一
                    QueryWrapper<UserBag> userBagQueryWrapper=new QueryWrapper<>();
                    userBagQueryWrapper.eq("user_id",user.getId());
                    UserBag userBag=userBagService.getOne(userBagQueryWrapper);
                    userBag.setUsedAmount(userBag.getUsedAmount()+1);
                    //保存
                    bagContentService.save(bagContent1);
                    userBagService.saveOrUpdate(userBag);
                }else{
                    bagContent.setAmount(bagContent.getAmount()+1);
                    bagContentService.saveOrUpdate(bagContent);
                }
            }
        }


        if(type=="钻石抽奖"){
            PrizeDiamonds prize=prizeDiamondsService.getById(result);
            //判断奖品类型
            if(prize.getPrizeType()==1){
                //积分额度获取
                Integer points=Integer.parseInt(MyStringUtils.pullNumber(prize.getPrizeName()));
                user.setPoints(user.getPoints()+ points);
            }
            if(prize.getPrizeType()==2||prize.getPrizeType()==3||prize.getPrizeType()==4){
                QueryWrapper<BagContent> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("user_id",user.getId());
                queryWrapper.eq("goods_name",prize.getPrizeName());
                BagContent bagContent=bagContentService.getOne(queryWrapper);
                //判断有无这个道具,没有则新加一条数据
                if(bagContent==null){
                    //得到道具详情信息
                    QueryWrapper<PointsStore> pointsStoreQueryWrapper=new QueryWrapper<>();
                    pointsStoreQueryWrapper.eq("goods_name",prize.getPrizeName());
                    PointsStore goods=pointsStoreService.getOne(pointsStoreQueryWrapper);
                    //添加背包内容物
                    BagContent bagContent1=new BagContent();
                    bagContent1.setGoodsId(goods.getId());
                    bagContent1.setGoodsName(goods.getGoodsName());
                    bagContent1.setAmount(1);
                    bagContent1.setUserId(user.getId());
                    //背包加一
                    QueryWrapper<UserBag> userBagQueryWrapper=new QueryWrapper<>();
                    userBagQueryWrapper.eq("user_id",user.getId());
                    UserBag userBag=userBagService.getOne(userBagQueryWrapper);
                    userBag.setUsedAmount(userBag.getUsedAmount()+1);
                    //保存
                    bagContentService.save(bagContent1);
                    userBagService.saveOrUpdate(userBag);
                }else{
                    bagContent.setAmount(bagContent.getAmount()+1);
                    bagContentService.saveOrUpdate(bagContent);
                }
            }
        }


    }
}
